#!/bin/bash

# 交互式API测试工具
# 提供菜单驱动的API测试界面

set -e

# 配置
API_BASE_URL="http://php-test:9000/api"
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 全局变量
CURRENT_TOKEN=""
CURRENT_USER_TYPE=""

# 显示菜单
show_menu() {
    clear
    echo -e "${BLUE}========================================${NC}"
    echo -e "${BLUE}    XAppTool API 交互式测试工具${NC}"
    echo -e "${BLUE}========================================${NC}"
    echo ""
    echo "1. 健康检查和系统状态"
    echo "2. 用户认证测试"
    echo "3. 商户认证测试"
    echo "4. 应用管理测试"
    echo "5. 会员系统测试"
    echo "6. SDK管理测试"
    echo "7. 数据统计测试"
    echo "8. 安全测试"
    echo "9. 性能测试"
    echo "10. 运行所有测试"
    echo "11. 查看测试日志"
    echo "12. 退出"
    echo ""
    echo -e "当前状态: ${YELLOW}Token: ${CURRENT_TOKEN:0:20}...${NC}"
    echo -e "用户类型: ${YELLOW}${CURRENT_USER_TYPE}${NC}"
    echo ""
    echo -n "请选择测试项目 (1-12): "
}

# 发送API请求
api_request() {
    local method="$1"
    local endpoint="$2"
    local data="$3"
    local auth_header=""
    
    if [ -n "$CURRENT_TOKEN" ]; then
        auth_header="-H \"Authorization: Bearer $CURRENT_TOKEN\""
    fi
    
    local cmd="curl -s -w \"\n%{http_code}\" $auth_header"
    
    if [ "$method" != "GET" ]; then
        cmd="$cmd -X $method -H \"Content-Type: application/json\" -d '$data'"
    fi
    
    cmd="$cmd $API_BASE_URL$endpoint"
    
    # 执行请求
    local response=$(eval "$cmd")
    local http_code=$(echo "$response" | tail -n1)
    local body=$(echo "$response" | head -n-1)
    
    echo "$http_code"
    echo "$body"
}

# 显示响应
display_response() {
    local http_code="$1"
    local body="$2"
    local expected_code="$3"
    
    echo -e "\n${YELLOW}=== 响应结果 ===${NC}"
    echo "HTTP状态码: $http_code"
    
    if [ "$http_code" = "$expected_code" ]; then
        echo -e "结果: ${GREEN}✓ 成功${NC}"
    else
        echo -e "结果: ${RED}✗ 失败${NC}"
        echo "期望状态码: $expected_code"
    fi
    
    if [ -n "$body" ]; then
        echo "响应内容:"
        echo "$body" | jq . 2>/dev/null || echo "$body"
    fi
    
    echo ""
    echo "按任意键继续..."
    read -n 1 -s
}

# 健康检查测试
health_check_menu() {
    clear
    echo -e "${BLUE}=== 健康检查和系统状态 ===${NC}"
    echo ""
    echo "1. 健康检查"
    echo "2. 性能指标"
    echo "3. 系统状态"
    echo "4. 返回主菜单"
    echo ""
    echo -n "请选择 (1-4): "
    
    read choice
    case $choice in
        1)
            echo -e "\n${YELLOW}正在执行健康检查...${NC}"
            local response=$(api_request "GET" "/health" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        2)
            echo -e "\n${YELLOW}正在获取性能指标...${NC}"
            local response=$(api_request "GET" "/performance/metrics" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        3)
            echo -e "\n${YELLOW}正在获取系统状态...${NC}"
            local response=$(api_request "GET" "/performance/status" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        4)
            return
            ;;
    esac
    health_check_menu
}

# 用户认证测试
user_auth_menu() {
    clear
    echo -e "${BLUE}=== 用户认证测试 ===${NC}"
    echo ""
    echo "1. 用户注册"
    echo "2. 用户登录"
    echo "3. 获取用户信息"
    echo "4. 用户登出"
    echo "5. 返回主菜单"
    echo ""
    echo -n "请选择 (1-5): "
    
    read choice
    case $choice in
        1)
            echo -e "\n${YELLOW}用户注册测试${NC}"
            echo -n "输入用户名: "
            read username
            echo -n "输入邮箱: "
            read email
            echo -n "输入密码: "
            read -s password
            echo ""
            
            local data="{\"username\":\"$username\",\"email\":\"$email\",\"password\":\"$password\",\"password_confirmation\":\"$password\"}"
            local response=$(api_request "POST" "/auth/register" "$data")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        2)
            echo -e "\n${YELLOW}用户登录测试${NC}"
            echo -n "输入邮箱: "
            read email
            echo -n "输入密码: "
            read -s password
            echo ""
            
            local data="{\"email\":\"$email\",\"password\":\"$password\"}"
            local response=$(api_request "POST" "/auth/login" "$data")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            
            if [ "$http_code" = "200" ]; then
                CURRENT_TOKEN=$(echo "$body" | jq -r '.data.token' 2>/dev/null || echo "")
                CURRENT_USER_TYPE="user"
                echo -e "${GREEN}登录成功，Token已保存${NC}"
            fi
            
            display_response "$http_code" "$body" "200"
            ;;
        3)
            if [ -z "$CURRENT_TOKEN" ]; then
                echo -e "\n${RED}请先登录${NC}"
                echo "按任意键继续..."
                read -n 1 -s
            else
                echo -e "\n${YELLOW}获取用户信息...${NC}"
                local response=$(api_request "GET" "/auth/user" "")
                local http_code=$(echo "$response" | head -n1)
                local body=$(echo "$response" | tail -n+2)
                display_response "$http_code" "$body" "200"
            fi
            ;;
        4)
            if [ -z "$CURRENT_TOKEN" ]; then
                echo -e "\n${RED}请先登录${NC}"
            else
                echo -e "\n${YELLOW}用户登出...${NC}"
                local response=$(api_request "POST" "/auth/logout" "")
                local http_code=$(echo "$response" | head -n1)
                local body=$(echo "$response" | tail -n+2)
                
                CURRENT_TOKEN=""
                CURRENT_USER_TYPE=""
                echo -e "${GREEN}已登出${NC}"
                display_response "$http_code" "$body" "200"
            fi
            echo "按任意键继续..."
            read -n 1 -s
            ;;
        5)
            return
            ;;
    esac
    user_auth_menu
}

# 商户认证测试
merchant_auth_menu() {
    clear
    echo -e "${BLUE}=== 商户认证测试 ===${NC}"
    echo ""
    echo "1. 商户注册"
    echo "2. 商户登录"
    echo "3. 获取商户信息"
    echo "4. 商户登出"
    echo "5. 返回主菜单"
    echo ""
    echo -n "请选择 (1-5): "
    
    read choice
    case $choice in
        1)
            echo -e "\n${YELLOW}商户注册测试${NC}"
            echo -n "输入商户名称: "
            read merchant_name
            echo -n "输入邮箱: "
            read email
            echo -n "输入密码: "
            read -s password
            echo ""
            
            local data="{\"merchant_name\":\"$merchant_name\",\"email\":\"$email\",\"password\":\"$password\",\"password_confirmation\":\"$password\"}"
            local response=$(api_request "POST" "/auth/merchant/register" "$data")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        2)
            echo -e "\n${YELLOW}商户登录测试${NC}"
            echo -n "输入邮箱: "
            read email
            echo -n "输入密码: "
            read -s password
            echo ""
            
            local data="{\"email\":\"$email\",\"password\":\"$password\"}"
            local response=$(api_request "POST" "/auth/merchant/login" "$data")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            
            if [ "$http_code" = "200" ]; then
                CURRENT_TOKEN=$(echo "$body" | jq -r '.data.token' 2>/dev/null || echo "")
                CURRENT_USER_TYPE="merchant"
                echo -e "${GREEN}登录成功，Token已保存${NC}"
            fi
            
            display_response "$http_code" "$body" "200"
            ;;
        3)
            if [ -z "$CURRENT_TOKEN" ]; then
                echo -e "\n${RED}请先登录${NC}"
                echo "按任意键继续..."
                read -n 1 -s
            else
                echo -e "\n${YELLOW}获取商户信息...${NC}"
                local response=$(api_request "GET" "/auth/merchant" "")
                local http_code=$(echo "$response" | head -n1)
                local body=$(echo "$response" | tail -n+2)
                display_response "$http_code" "$body" "200"
            fi
            ;;
        4)
            if [ -z "$CURRENT_TOKEN" ]; then
                echo -e "\n${RED}请先登录${NC}"
            else
                echo -e "\n${YELLOW}商户登出...${NC}"
                local response=$(api_request "POST" "/auth/merchant/logout" "")
                local http_code=$(echo "$response" | head -n1)
                local body=$(echo "$response" | tail -n+2)
                
                CURRENT_TOKEN=""
                CURRENT_USER_TYPE=""
                echo -e "${GREEN}已登出${NC}"
                display_response "$http_code" "$body" "200"
            fi
            echo "按任意键继续..."
            read -n 1 -s
            ;;
        5)
            return
            ;;
    esac
    merchant_auth_menu
}

# 应用管理测试
app_management_menu() {
    clear
    echo -e "${BLUE}=== 应用管理测试 ===${NC}"
    echo ""
    
    if [ -z "$CURRENT_TOKEN" ]; then
        echo -e "${RED}请先登录${NC}"
        echo "按任意键返回..."
        read -n 1 -s
        return
    fi
    
    echo "1. 创建应用"
    echo "2. 获取应用列表"
    echo "3. 获取应用详情"
    echo "4. 更新应用"
    echo "5. 删除应用"
    echo "6. 返回主菜单"
    echo ""
    echo -n "请选择 (1-6): "
    
    read choice
    case $choice in
        1)
            echo -e "\n${YELLOW}创建应用测试${NC}"
            echo -n "输入应用名称: "
            read app_name
            echo -n "输入应用描述: "
            read app_desc
            echo -n "输入平台 (web/mobile/desktop): "
            read platform
            
            local data="{\"app_name\":\"$app_name\",\"app_description\":\"$app_desc\",\"platform\":\"$platform\"}"
            local response=$(api_request "POST" "/apps" "$data")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        2)
            echo -e "\n${YELLOW}获取应用列表...${NC}"
            local response=$(api_request "GET" "/apps" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        3)
            echo -e "\n${YELLOW}获取应用详情${NC}"
            echo -n "输入应用ID: "
            read app_id
            
            local response=$(api_request "GET" "/apps/$app_id" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        4)
            echo -e "\n${YELLOW}更新应用${NC}"
            echo -n "输入应用ID: "
            read app_id
            echo -n "输入新的应用名称: "
            read app_name
            echo -n "输入新的应用描述: "
            read app_desc
            
            local data="{\"app_name\":\"$app_name\",\"app_description\":\"$app_desc\"}"
            local response=$(api_request "PUT" "/apps/$app_id" "$data")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        5)
            echo -e "\n${YELLOW}删除应用${NC}"
            echo -n "输入应用ID: "
            read app_id
            echo -n "确认删除应用 $app_id 吗? (y/N): "
            read confirm
            
            if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
                local response=$(api_request "DELETE" "/apps/$app_id" "")
                local http_code=$(echo "$response" | head -n1)
                local body=$(echo "$response" | tail -n+2)
                display_response "$http_code" "$body" "200"
            else
                echo -e "${YELLOW}操作已取消${NC}"
                echo "按任意键继续..."
                read -n 1 -s
            fi
            ;;
        6)
            return
            ;;
    esac
    app_management_menu
}

# 会员系统测试
membership_menu() {
    clear
    echo -e "${BLUE}=== 会员系统测试 ===${NC}"
    echo ""
    echo "1. 获取会员等级"
    echo "2. 获取会员权益"
    echo "3. 获取会员状态"
    echo "4. 返回主菜单"
    echo ""
    echo -n "请选择 (1-4): "
    
    read choice
    case $choice in
        1)
            echo -e "\n${YELLOW}获取会员等级...${NC}"
            local response=$(api_request "GET" "/membership/tiers" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        2)
            echo -e "\n${YELLOW}获取会员权益...${NC}"
            local response=$(api_request "GET" "/membership/benefits" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        3)
            if [ -z "$CURRENT_TOKEN" ]; then
                echo -e "\n${RED}请先登录${NC}"
                echo "按任意键继续..."
                read -n 1 -s
            else
                echo -e "\n${YELLOW}获取会员状态...${NC}"
                local response=$(api_request "GET" "/membership/status" "")
                local http_code=$(echo "$response" | head -n1)
                local body=$(echo "$response" | tail -n+2)
                display_response "$http_code" "$body" "200"
            fi
            ;;
        4)
            return
            ;;
    esac
    membership_menu
}

# SDK管理测试
sdk_menu() {
    clear
    echo -e "${BLUE}=== SDK管理测试 ===${NC}"
    echo ""
    echo "1. 获取SDK配置"
    echo "2. 获取SDK版本"
    echo "3. 获取SDK文档"
    echo "4. 返回主菜单"
    echo ""
    echo -n "请选择 (1-4): "
    
    read choice
    case $choice in
        1)
            echo -e "\n${YELLOW}获取SDK配置...${NC}"
            local response=$(api_request "GET" "/sdk/config" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        2)
            echo -e "\n${YELLOW}获取SDK版本...${NC}"
            local response=$(api_request "GET" "/sdk/versions" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        3)
            echo -e "\n${YELLOW}获取SDK文档...${NC}"
            local response=$(api_request "GET" "/sdk/docs" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        4)
            return
            ;;
    esac
    sdk_menu
}

# 数据统计测试
analytics_menu() {
    clear
    echo -e "${BLUE}=== 数据统计测试 ===${NC}"
    echo ""
    
    if [ -z "$CURRENT_TOKEN" ]; then
        echo -e "${RED}请先登录${NC}"
        echo "按任意键返回..."
        read -n 1 -s
        return
    fi
    
    echo "1. 应用统计"
    echo "2. 用户统计"
    echo "3. 商户统计"
    echo "4. 系统统计"
    echo "5. 返回主菜单"
    echo ""
    echo -n "请选择 (1-5): "
    
    read choice
    case $choice in
        1)
            echo -e "\n${YELLOW}获取应用统计...${NC}"
            local response=$(api_request "GET" "/analytics/apps/stats" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        2)
            echo -e "\n${YELLOW}获取用户统计...${NC}"
            local response=$(api_request "GET" "/analytics/users/stats" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        3)
            echo -e "\n${YELLOW}获取商户统计...${NC}"
            local response=$(api_request "GET" "/analytics/merchants/stats" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        4)
            echo -e "\n${YELLOW}获取系统统计...${NC}"
            local response=$(api_request "GET" "/analytics/system/stats" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        5)
            return
            ;;
    esac
    analytics_menu
}

# 安全测试
security_menu() {
    clear
    echo -e "${BLUE}=== 安全测试 ===${NC}"
    echo ""
    echo "1. 速率限制测试"
    echo "2. SQL注入防护测试"
    echo "3. XSS防护测试"
    echo "4. 认证绕过测试"
    echo "5. 返回主菜单"
    echo ""
    echo -n "请选择 (1-5): "
    
    read choice
    case $choice in
        1)
            echo -e "\n${YELLOW}速率限制测试...${NC}"
            echo "正在发送15个快速请求以测试速率限制..."
            
            local passed=true
            for i in {1..15}; do
                local response=$(api_request "GET" "/health" "")
                local http_code=$(echo "$response" | head -n1)
                
                if [ $i -gt 10 ] && [ "$http_code" != "429" ]; then
                    passed=false
                    break
                fi
                
                echo -n "."
                sleep 0.1
            done
            
            echo ""
            if [ "$passed" = true ]; then
                echo -e "${GREEN}✓ 速率限制测试通过${NC}"
            else
                echo -e "${RED}✗ 速率限制测试失败${NC}"
            fi
            echo "按任意键继续..."
            read -n 1 -s
            ;;
        2)
            echo -e "\n${YELLOW}SQL注入防护测试${NC}"
            local data='{"email":"test@example.com\' OR 1=1--","password":"test"}'
            local response=$(api_request "POST" "/auth/login" "$data")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "400"
            ;;
        3)
            echo -e "\n${YELLOW}XSS防护测试${NC}"
            local data='{"username":"<script>alert(\"xss\")</script>","email":"xss@test.com","password":"test123"}'
            local response=$(api_request "POST" "/auth/register" "$data")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        4)
            echo -e "\n${YELLOW}认证绕过测试${NC}"
            local old_token="$CURRENT_TOKEN"
            CURRENT_TOKEN="invalid_token_12345"
            
            local response=$(api_request "GET" "/auth/user" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            
            CURRENT_TOKEN="$old_token"
            display_response "$http_code" "$body" "401"
            ;;
        5)
            return
            ;;
    esac
    security_menu
}

# 性能测试
performance_menu() {
    clear
    echo -e "${BLUE}=== 性能测试 ===${NC}"
    echo ""
    echo "1. 并发请求测试"
    echo "2. 响应时间测试"
    echo "3. 内存使用测试"
    echo "4. 数据库性能测试"
    echo "5. 返回主菜单"
    echo ""
    echo -n "请选择 (1-5): "
    
    read choice
    case $choice in
        1)
            echo -e "\n${YELLOW}并发请求测试${NC}"
            echo "正在执行10个并发请求..."
            
            local start_time=$(date +%s%N)
            
            for i in {1..10}; do
                api_request "GET" "/health" "" > /dev/null 2>&1 &
            done
            
            wait
            local end_time=$(date +%s%N)
            local duration=$(( (end_time - start_time) / 1000000 ))
            
            echo -e "${GREEN}✓ 并发测试完成${NC}"
            echo "总耗时: ${duration}ms"
            echo "平均响应时间: $((duration / 10))ms"
            echo "按任意键继续..."
            read -n 1 -s
            ;;
        2)
            echo -e "\n${YELLOW}响应时间测试${NC}"
            echo "正在测试各个端点的响应时间..."
            
            local endpoints=("/health" "/performance/metrics" "/membership/tiers" "/sdk/config")
            
            for endpoint in "${endpoints[@]}"; do
                local start_time=$(date +%s%N)
                local response=$(api_request "GET" "$endpoint" "")
                local end_time=$(date +%s%N)
                local duration=$(( (end_time - start_time) / 1000000 ))
                
                echo "$endpoint: ${duration}ms"
            done
            
            echo "按任意键继续..."
            read -n 1 -s
            ;;
        3)
            echo -e "\n${YELLOW}内存使用测试${NC}"
            local response=$(api_request "GET" "/performance/status" "")
            local http_code=$(echo "$response" | head -n1)
            local body=$(echo "$response" | tail -n+2)
            display_response "$http_code" "$body" "200"
            ;;
        4)
            echo -e "\n${YELLOW}数据库性能测试${NC}"
            echo "正在测试数据库查询性能..."
            
            local start_time=$(date +%s%N)
            for i in {1..5}; do
                api_request "GET" "/membership/tiers" "" > /dev/null 2>&1
            done
            local end_time=$(date +%s%N)
            local duration=$(( (end_time - start_time) / 1000000 ))
            
            echo -e "${GREEN}✓ 数据库性能测试完成${NC}"
            echo "5次查询总耗时: ${duration}ms"
            echo "平均查询时间: $((duration / 5))ms"
            echo "按任意键继续..."
            read -n 1 -s
            ;;
        5)
            return
            ;;
    esac
    performance_menu
}

# 查看测试日志
view_logs() {
    clear
    echo -e "${BLUE}=== 测试日志 ===${NC}"
    echo ""
    
    if [ -f "/var/www/test-results/api_test_report_*.json" ]; then
        echo "最近的测试报告:"
        ls -la /var/www/test-results/api_test_report_*.json 2>/dev/null | head -5
        echo ""
        echo -n "是否要查看最新的测试报告? (y/N): "
        read view_choice
        
        if [ "$view_choice" = "y" ] || [ "$view_choice" = "Y" ]; then
            local latest_report=$(ls -t /var/www/test-results/api_test_report_*.json 2>/dev/null | head -1)
            if [ -n "$latest_report" ]; then
                echo ""
                cat "$latest_report" | jq . 2>/dev/null || cat "$latest_report"
            fi
        fi
    else
        echo "没有找到测试报告文件"
    fi
    
    echo ""
    echo "按任意键继续..."
    read -n 1 -s
}

# 运行所有测试
run_all_tests() {
    clear
    echo -e "${BLUE}=== 运行所有测试 ===${NC}"
    echo ""
    echo -e "${YELLOW}这将运行完整的API测试套件${NC}"
    echo -n "确认要继续吗? (y/N): "
    
    read confirm
    if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
        echo ""
        echo "正在运行完整测试套件..."
        bash /var/www/test-scripts/run_api_tests.sh
        echo ""
        echo -e "${GREEN}测试完成!${NC}"
        echo "按任意键继续..."
        read -n 1 -s
    fi
}

# 主循环
main() {
    while true; do
        show_menu
        read choice
        
        case $choice in
            1) health_check_menu ;;
            2) user_auth_menu ;;
            3) merchant_auth_menu ;;
            4) app_management_menu ;;
            5) membership_menu ;;
            6) sdk_menu ;;
            7) analytics_menu ;;
            8) security_menu ;;
            9) performance_menu ;;
            10) run_all_tests ;;
            11) view_logs ;;
            12) 
                echo -e "\n${GREEN}感谢使用XAppTool API测试工具!${NC}"
                exit 0
                ;;
            *)
                echo -e "\n${RED}无效选择，请重试${NC}"
                echo "按任意键继续..."
                read -n 1 -s
                ;;
        esac
    done
}

# 如果直接运行此脚本
if [ "${BASH_SOURCE[0]}" == "${0}" ]; then
    main "$@"
fi